package com.adv.ems.manager.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.adv.ems.db.mapper.EnergyDataCumulateReportMonthMapper;
import com.adv.ems.db.entity.EnergyDataCumulateReportMonthDO;
import com.adv.ems.manager.EnergyDataCumulateReportMonthManager;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
public class EnergyDataCumulateReportMonthManagerImpl extends ServiceImpl<EnergyDataCumulateReportMonthMapper, EnergyDataCumulateReportMonthDO> implements EnergyDataCumulateReportMonthManager {
    @Override
    public void deleteByPointColDate(Long pointId, Date month) {
        LambdaUpdateWrapper<EnergyDataCumulateReportMonthDO> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(EnergyDataCumulateReportMonthDO::getCollectPointId, pointId);
        wrapper.eq(EnergyDataCumulateReportMonthDO::getColDate, month);
        remove(wrapper);
    }

    @Override
    public void insertData(EnergyDataCumulateReportMonthDO reportMonth) {
        save(reportMonth);
    }

    @Override
    public List<EnergyDataCumulateReportMonthDO> getAllDataByPonitTime(Long pointId, Date start, Date end) {
        LambdaQueryWrapper<EnergyDataCumulateReportMonthDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(EnergyDataCumulateReportMonthDO::getCollectPointId, pointId);
        wrapper.ge(EnergyDataCumulateReportMonthDO::getColDate, start);
        wrapper.le(EnergyDataCumulateReportMonthDO::getColDate, end);
        wrapper.orderByAsc(EnergyDataCumulateReportMonthDO::getColDate);
        return list(wrapper);
    }

    @Override
    public List<EnergyDataCumulateReportMonthDO> selectData(Date startDate, Date endDate, List<Long> pointIds) {
        LambdaQueryWrapper<EnergyDataCumulateReportMonthDO> wrapper = new LambdaQueryWrapper<>();
        if (CollectionUtils.isEmpty(pointIds)) {
            return new ArrayList<>();
        }
        wrapper.in(EnergyDataCumulateReportMonthDO::getCollectPointId, pointIds);
        wrapper.ge(EnergyDataCumulateReportMonthDO::getColDate, startDate);
        wrapper.le(EnergyDataCumulateReportMonthDO::getColDate, endDate);
        return list(wrapper);
    }
}
